﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

using System.Threading.Tasks;
using System.Windows.Documents;

namespace ETask.Client.Models
{
    public class TaskDataService
    {

        // 数据存储路径：用户AppData目录
        private readonly string _dataFilePath;

        public static TaskDataService Instance { get; } = new TaskDataService();

        public ObservableCollection<TaskItem> Tasks { get; } = new ObservableCollection<TaskItem>();

        public event EventHandler<TaskItem> AddEvent;

        public event EventHandler<TaskItem> DelEvent;

        public event EventHandler<TaskItem> UpdateEvent;

        private TaskDataService()
        {
            // 获取应用数据存储目录
            string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string appFolder = Path.Combine(appDataPath, "TaskManager");

            // 确保目录存在
            if (!Directory.Exists(appFolder))
            {
                Directory.CreateDirectory(appFolder);
            }

            _dataFilePath = Path.Combine(appFolder, "tasks.json");
            if (!File.Exists(_dataFilePath))
            {
                File.WriteAllText(_dataFilePath, "");
            }
        }

        public async void AddTask(TaskItem task)
        {
            Tasks.Add(task);
            string json = JsonConvert.SerializeObject(Tasks);
            await File.WriteAllTextAsync(_dataFilePath, json);
            if (AddEvent !=null)
            {
                AddEvent(this, task);
            }
        }

        /// <summary>
        /// 任务初始化
        /// </summary>
        public void Init()
        {
            string content =  File.ReadAllText(_dataFilePath);
    
            try
            {
                var list = JsonConvert.DeserializeObject<List<TaskItem>>(content);
                if (list == null)
                {
                    return;
                }
                foreach (var item in list)
                {
                    Tasks.Add(item);
                }
            }
            catch (Exception)
            {
                File.WriteAllText(_dataFilePath, "");
            }
        }

        public async void RemoveTask(TaskItem task)
        {
            Tasks.Remove(task);
            string json = JsonConvert.SerializeObject(Tasks);
            await File.WriteAllTextAsync(_dataFilePath, json);
            if (DelEvent !=null)
            {
                DelEvent(this, task);
            }
        }

        public async void UpdateTask(TaskItem task)
        {
            string json = JsonConvert.SerializeObject(Tasks);
            await File.WriteAllTextAsync(_dataFilePath, json);
            if (UpdateEvent != null)
            {
                UpdateEvent(this, task);
            }
            
        }

        public List<TaskItem> GetTaskItems(DateTime dateTime)
        {
            return Tasks.Where(q => q.TaskDate.Date == dateTime.Date).ToList();
        }

    }
}
